import { menus } from '@/pages/home/home.config';
import { connect, Redirect } from 'umi'
function auth(props: any) {

    const currentMenus = (newMenus = menus) => {
        return newMenus.filter((item: any) => {
            if (item.children) {
                item.children && currentMenus(item.children)
            }
            return item.roles.some((val: any) => props.roles.includes(val))
        })
    }
    const routerArr = currentMenus()
    const example = JSON.parse(localStorage.getItem('example') || "{}")

    if (!!example.token && routerArr.some(item => {
        if(item.children){
            return item.children.some(citem=>{
                return citem.key.includes(props.location.pathname)
            })
        }
        return item.key.includes(props.location.pathname)
    })) {
        return (
            <div>
                {props.children}
            </div>
        )
    }

    return <Redirect to="/login" />
}

export default connect((state: any) => {
    return {
        roles: state.example.roles
    }
})(auth)